package com.twu.biblioteca;

import java.util.HashMap;
import java.util.Map;

public class ReservationSystem {
    Map<Book,ReservationStats> bookList;

    public ReservationSystem(){
        bookList = new HashMap<Book, ReservationStats>();
    }

    public void add(Book book) {
        int reservableCount = getReservableCount(book);
        bookList.put(book, new ReservationStats(reservableCount + 1));
    }

    public boolean reserve(Book book) {
        int reservableCount = getReservableCount(book);
        if(reservableCount == 0)
            return false;
        bookList.put(book, new ReservationStats(reservableCount - 1));
        return true;
    }

    private int getReservableCount(Book book) {
        return bookList.containsKey(book) ? bookList.get(book).getReservableCount() : 0;
    }

    public String allBooksString() {
        String returnMe = "";
        for(Book book: bookList.keySet())
            returnMe += ", " + book.toString();
        return returnMe.trim().replaceFirst(", ", "");
    }
}
